package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author chensheng
 * @since 2025-08-08
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 分类分页查询
     *
     * @param cDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO cDTO) {

        Page<Category> page = new Page<>(cDTO.getPage(), cDTO.getPageSize());
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.like(cDTO.getName()!=null, Category::getName, cDTO.getName());
        qw.eq(cDTO.getType()!=null,Category::getType,cDTO.getType());
        qw.orderByAsc(Category::getSort);
        Page<Category> categoryPage = categoryMapper.selectPage(page, qw);
        return PageResult.builder()
                .total(categoryPage.getTotal())
                .records(categoryPage.getRecords())
                .build();
    }

    /**
     * 根据类型分类查询
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> listByType(Integer type) {
        return categoryMapper.selectByType(type);
    }

    /**
     * 修改分类
     *
     * @param categoryDTO
     */
    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.updateById(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        this.categoryMapper.updateById(category);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void saveCategory(CategoryDTO categoryDTO) {
        //DTO 转换为实体类
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        //补全数据
        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }
}